<!--
 * @Description: 合并批次组弹窗
 * @Author: liyujie
 * @Date: 2021/2/23
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/2/23
-->

<template>
    <section class="wrapper">
        <el-dialog
            title="合并批次组"
            width="920px"
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
            @close="handleClose"
        >
            <!-- table -->
            <el-table
                :data="tableData"
                height="400"
                style="width: 100%;"
                ref="tempLateName"
                class="data-table"
            >
                <el-table-column
                    prop="title"
                    label="批次组名称"
                    show-overflow-tooltip
                    align="left"
                    width="200"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="school_name"
                    label="学校"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="count"
                    label="寝室推广数量"
                    show-overflow-tooltip
                    align="center"
                    width="110"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="寝室性别"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.sex === 1 ? '男' : scope.row.sex === 2 ? '女' : '不限' }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="计划推广时间"
                    align="center"
                    :formatter="emptyFormatter"
                    width="200"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.schedule_list && scope.row.schedule_list.length === 0">--</div>
                        <div v-if="scope.row.schedule_list.length >= 1">
                            {{ scope.row.schedule_list[0]['start_time'] * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ scope.row.schedule_list[0]['end_time'] * 1000 | dateFormatter('yyyy/MM/dd') }}
                        </div>
                        <div v-if="scope.row.schedule_list.length === 2">
                            <div>{{ scope.row.schedule_list[1]['start_time'] * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ scope.row.schedule_list[1]['end_time'] * 1000 | dateFormatter('yyyy/MM/dd') }}</div>
                        </div>
                        <div v-if="scope.row.schedule_list.length > 2">
                            <el-popover
                                placement="bottom"
                                width="246"
                                trigger="click"
                                popper-class="date-list-popper"
                            >
                                <div
                                    v-for="(row,index) in scope.row.schedule_list.slice(1)"
                                    :key="index"
                                    class="date-item"
                                >
                                    {{ index + 1 }}、{{ row['start_time'] * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ row['end_time'] * 1000 | dateFormatter('yyyy/MM/dd') }}
                                </div>
                                <el-button
                                    slot="reference"
                                    type="text"
                                >
                                    查看更多
                                </el-button>
                            </el-popover>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="推广状态"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.status | statusFilter }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                >
                    <template slot-scope="scope">
                        <div class="option-btn-group">
                            <el-button
                                type="text"
                                size="medium"
                                @click="handleDelete(scope.$index)"
                            >
                                删除
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->
            <div class="btn">
                <el-button
                    size="medium"
                    style="width: 120px;margin-right: 32px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    size="medium"
                    type="primary"
                    style="width: 120px"
                    :disabled="tableData.length === 0"
                    @click="handleSubmit"
                >
                    确认
                </el-button>
            </div>
        </el-dialog>
        <!--修改批次组名称-->
        <series-title-edit
            ref="editTitle"
            @confirm="handleMerge(series_nos, $event)"
        />
    </section>
</template>

<script>
import { deepClone } from "@/assets/js/utils";
import SeriesTitleEdit from "./series-title-edit";

export default {
    name: "MergeSeriesGroup",
    components: {SeriesTitleEdit},
    data() {
        return {
            dialogVisible: false,
            tableData: [],
            series_nos: ''
        };
    },
    filters: {
        statusFilter(val) {
            switch (val) {
                case 1:
                    return '排期中';
                case 2:
                    return '排队中';
                case 3:
                    return '进行中';
                case 4:
                    return '已完成';
                case 5:
                    return '已终止';
                case 6:
                    return '已暂停';
                default:
                    return '--';
            }
        },
    },
    mounted() {},
    methods: {
        /**
         * 显示弹窗
         * @param data
         */
        show(data) {
            this.tableData = data;
            this.dialogVisible = true;
        },

        /**
         * 提交检查是否可合并
         */
        handleSubmit() {
            let data = deepClone(this.tableData);
            let series_nos = data.map(item => {
                return item.series_no;
            }).join();

            this.$post('/student/room_series%5Ccheck_can_merge_series', {series_nos}, resp => {
                if (resp.code === 1) {
                    let message;
                    // 可合并

                    if(resp.data.result === 1) {
                        this.$confirm('合并后，批次组内的视频将合并在一起，是否将当前选中的所有批次组合并?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            this.series_nos = series_nos;
                            this.$refs.editTitle.show('批次组新名称');
                        }).catch(() => {});
                    } else {
                        switch (resp.data.result) {
                            case 0:
                                message = '合并失败';
                                break;
                            case -1:
                                message = '信息不一致';
                                break;
                            case -2:
                                message = '排期不一致';
                                break;
                            case -3:
                                message = '进度不一致';
                                break;
                            case -4:
                                message = '多个批次已委派';
                                break;
                            default:
                                message = '未知错误';
                                break;
                        }
                        this.$notify({
                            title: '提示',
                            message: message,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * 合并批次组
         */
        handleMerge(series_nos, title) {
            this.$post('/student/room_series%5Cmerge_series', {series_nos, title}, resp => {
                if (resp.code === 1) {
                    this.$emit('update');
                    this.dialogVisible = false;
                    this.$notify({
                        title: '提示',
                        message: '合并成功',
                        type: 'success',
                        duration: 2000
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * 弹窗关闭 重置表单
         */
        handleClose() {
            this.tableData = [];
            this.$emit('close');
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 删除
         * @param index
         */
        handleDelete(index) {
            this.tableData.splice(index, 1);
        },

        /**
         * 设置批次组名称
         * @param series_no
         * @param title
         */
        setSeriesTitle(series_no, title) {
            let params = {
                series_no,
                title
            };

            this.$post('/student/room_series%5Cupdate', params, resp => {
                if (resp.code === 1) {
                    this.$emit('update');
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

    }
};
</script>

<style scoped lang="less">
.wrapper {
    .tips {
        color: #999999;
        font-size: 12px;
        margin-left: 12px;
    }
    // 表格
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-dialog__body {
        padding: 48px 36px;
        max-height: 600px;
        overflow: auto;
        &::-webkit-scrollbar {
            width: 6px;
        }
    }
    .btn {
        text-align: center;
        margin-top: 36px;
    }
}
</style>
